﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using System.Collections.Generic;
using System.Web.Services;
using Meqs.Utilities;

public partial class School_Report_ClassSchoolReport : System.Web.UI.Page
{
    //protected string iseexam = null;
    //protected string id = null;
    //protected string ct = null;
    //protected string cid = null;
    //protected string g = null;

    /// <summary>
    /// 考试号
    /// </summary>
    protected int examid = 0;
    /// <summary>
    /// 是否为统考 -1错误，1是，0不是
    /// </summary>
    protected int eexam = -1;
    /// <summary>
    /// 班级分科
    /// </summary>
    protected int classType = -1;
    /// <summary>
    /// 班级id
    /// </summary>
    protected int classId = 0;
    /// <summary>
    /// 班级名
    /// </summary>
    protected string clsname = "";
    /// <summary>
    /// 年级
    /// </summary>
    protected int Grade = 0;

    /// <summary>
    /// 本次考试的科目(统考)
    /// </summary>
    protected IList<ExamSubjecyEducation2> subjEdu = null;
    /// <summary>
    /// 本次考试的科目(校考)
    /// </summary>
    protected IList<ExamSubjecySchool2> subjSchool = null;
    /// <summary>
    /// 学生列表信息
    /// </summary>
    protected IList<StudentInfo> studentInfo = null;
    /// <summary>
    /// 统考成绩列表（已经输入的）
    /// </summary>
    protected IList<GradeScoreEducation> gse = null;
    /// <summary>
    /// 校考试成绩
    /// </summary>
    protected IList<ExamDetailSchool> eds = null;

    protected List<double> scores = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Textid.Text = SchoolCookie.ExamID.ToString();
            Textiseexam.Text = SchoolCookie.ExamType.ToString();
            Textg.Text = SchoolCookie.ExamGrade.ToString();
        }
    }

    protected string GetHeader()
    {
        int w = (800 - 160);
        int index = 2;
        string header = "<th width='80px' style='text-align:left;padding-left: 5px;'>学号</th><th width='80px' style='text-align:left;padding-left: 5px;'>学生</th>";
        if (eexam == 1)
        {
            if (subjEdu != null)
            {

                w = (subjEdu.Count == 0 ? 0 : w / (subjEdu.Count + 1));
                foreach (ExamSubjecyEducation2 ese in subjEdu)
                {
                    header += "<th width='" + w + "px' style='text-align:right;padding-right: 5px;cursor: hand;' onclick='sort(" + index + ")'>" + ese.SubjectName + "<span  id='img" + index + "'></span></th>";
                    index++;
                }
                if (subjEdu.Count >= 0)
                {
                    header += "<th width='" + w + "px' style='text-align:right;padding-right: 5px;cursor: hand;' onclick='sort(" + index + ")'>总分<span id='img" + index + "'><img src='../../Common/images/1.gif' /></span></th>";
                    index++;
                }
            }

        }
        else
        {
            if (subjSchool != null)
            {
                w = (subjSchool.Count == 0 ? 0 : w / (subjSchool.Count + 1));
                foreach (ExamSubjecySchool2 ese in subjSchool)
                {
                    header += "<th width='" + w + "' style='text-align:right;padding-right: 5px;cursor: hand;' onclick='sort(" + index + ")'>" + ese.SubjectName + "<span  id='img" + index + "'></span></th>";
                    index++;
                }
                if (subjSchool.Count >= 0)
                {
                    header += "<th width='" + w + "px' style='text-align:right;padding-right: 5px;cursor: hand;'  onclick='sort(" + index + ")'>总分<span onclick='' id='img" + index + "' ><img src='../../Common/images/1.gif' /></span></th>";
                    index++;
                }
            }
        }
        return header;

    }

    protected string GetStudent(int stuid, string StudentCode, string StudentName)
    {
        string header = "<tr  style='background-color: White;'  onclick='SetHightLight(this);'>";

        int w = (800 - 160);
        int index = 0;
        header += "<td width='80px' style='text-align:left;padding-left: 5px;'>" + StudentCode + "</td><td width='80px' style='text-align:left;padding-left: 5px;'>" + StudentName + "</td>";
        if (eexam == 1)
        {
            if (subjEdu != null)
            {
                w = (subjEdu.Count == 0 ? 0 : w / (1 + subjEdu.Count));
                double fullscore = 0;
                foreach (ExamSubjecyEducation2 ese in subjEdu)
                {
                    string score = GetTongKaoScore(stuid, ese.SubjectID);
                    scores[index] += Convert.ToDouble((score == "null" ? "0" : score));
                    index++;
                    fullscore += Convert.ToDouble((score == "null" ? "0" : score));
                    header += "<td width='" + w + "px' style='text-align:right;padding-right: 5px;'>" + (score == "null" ? "" : score) + "</td>";
                }
                if (subjEdu.Count >= 0)
                {
                    scores[index] += fullscore;
                    index++;
                    header += "<td width='" + w + "px' style='text-align:right;padding-right: 5px;'>" + fullscore + "</td>";
                }
            }
        }
        else
        {
            if (subjSchool != null)
            {
                w = (subjSchool.Count == 0 ? 0 : w / (1 + subjSchool.Count));
                double fullscore = 0;
                foreach (ExamSubjecySchool2 ese in subjSchool)
                {
                    string score = GetXiaoKaoScore(stuid, ese.SubjectID);
                    scores[index] += Convert.ToDouble((score == "null" ? "0" : score));
                    index++;
                    fullscore += Convert.ToDouble((score == "null" ? "0" : score));
                    header += "<td width='" + w + "px' style='text-align:right;padding-right: 5px;'>" + (score == "null" ? "" : score) + "</td>";
                }
                if (subjSchool.Count >= 0)
                {
                    scores[index] += fullscore;
                    index++;
                    header += "<td width='" + w + "px' style='text-align:right;padding-right: 5px;'>" + fullscore + "</td>";
                }
            }
        }


        header += "</tr>";
        return header;
    }

    protected string GetFooter()
    {
        string str = "";
        int w = 640 / scores.Count;
        foreach (double d in scores)
        {
            str += "<td width='" + w + "px' style='text-align:right;padding-right: 5px;font-weight:normal;font-weight:bold;'>" + Math.Round(studentInfo.Count == 0 ? 0 : d / studentInfo.Count, 1) + "</td>";

        }
        return str;
    }
    /// <summary>
    /// 获得某学生的统考分数
    /// </summary>
    /// <param name="stuId"></param>
    /// <param name="subjId"></param>
    /// <returns></returns>
    private string GetTongKaoScore(int stuId, int subjId)
    {

        foreach (GradeScoreEducation g in gse)
        {
            if (g.StudentID == stuId && g.SubjectID == subjId)
            {
                return (g.Score == null) ? "null" : g.Score.ToString();
            }
        }
        return "null";
    }
    /// <summary>
    /// 获得某学生的校考分数
    /// </summary>
    /// <param name="stuId"></param>
    /// <param name="subjId"></param>
    /// <returns></returns>
    private string GetXiaoKaoScore(int stuId, int subjId)
    {
        foreach (ExamDetailSchool e in eds)
        {
            if (e.StudentID == stuId && e.SubjectID == subjId)
            {
                return (e.Score == null) ? "null" : e.Score.ToString();
            }
        }
        return "null";
    }
    protected void btnBind_Click(object sender, EventArgs e)
    {

        string iseexam = Textiseexam.Text;
        string id = Textid.Text;
        string ct = Textct.Text;
        string cid = Textcid.Text;
        string g = Textg.Text;
        int temp = 0;
        if (int.TryParse(id, out temp))
        {
            examid = temp;
        }

        temp = -1;
        if (int.TryParse(iseexam, out temp))
        {
            eexam = temp;
        }
        temp = -1;
        if (int.TryParse(ct, out temp))
        {
            classType = temp;
        }
        temp = 0;
        if (int.TryParse(cid, out temp))
        {
            classId = temp;
            ClsInfo tcl = Container<Meqs.Logic.Interface.IClsInfoLogic>.Instance.FindById(classId);
            clsname = tcl.ClsName;
            classType = SchoolCookie.ExamClassType;
        }
        temp = 0;
        if (int.TryParse(g, out temp))
        {
            Grade = temp;
        }
        if (eexam != -1 && examid != 0)
        {
            Hashtable table = new Hashtable();
            table.Add("ClsID", classId);
            //studentInfo = Container<Meqs.Logic.Interface.IStudentInfoLogic>.Instance.FindAll(table);

            if (eexam == 1)
            {
                table = new Hashtable();
                table.Add("TableName", CommonScore.GetGradeStoreTable(Grade));
                table.Add("Examid", examid);
                table.Add("ClsID", classId);
                studentInfo = Container<Meqs.Logic.Interface.IExamsEducationLogic>.Instance.FindStudent(table);
                subjEdu = Container<Meqs.Logic.Interface.IExamSubjecyEducationLogic>.Instance.GetExamCause(examid, classType);
                scores = new List<double>();
                for (int ii = 0; ii < subjEdu.Count; ii++)
                {
                    scores.Add(0);
                }
                scores.Add(0);
                table = new Hashtable();
                table.Add("TableName", CommonScore.GetGradeStoreTable(Grade));
                table.Add("SchoolMark", CommonScore.GetSchoolMark);
                table.Add("ExamID", examid);
                table.Add("ClsID", classId);
                gse = Container<Meqs.Logic.Interface.IGradeScoreEducationLogic>.Instance.FindAll(table);
            }
            else
            {
                table = new Hashtable();
                table.Add("TableName", CommonScore.GetSchoolMark + ".examdetail");
                table.Add("Examid", examid);
                table.Add("ClsID", classId);
                studentInfo = Container<Meqs.Logic.Interface.IExamsEducationLogic>.Instance.FindStudent(table);

                subjSchool = Container<Meqs.Logic.Interface.IExamSubjecySchoolLogic>.Instance.GetExamCause(examid, classType, CommonScore.GetSchoolMark);
                scores = new List<double>();
                for (int ii = 0; ii < subjSchool.Count; ii++)
                {
                    scores.Add(0);
                }
                scores.Add(0);
                table = new Hashtable();
                table.Add("SchoolMark", CommonScore.GetSchoolMark);
                table.Add("ExamID", examid);
                table.Add("ClsID", classId);
                eds = Container<Meqs.Logic.Interface.IExamDetailSchoolLogic>.Instance.FindAll(table);
            }
            SortForFull();

            Repeater1.DataSource = studentInfo;
            Repeater1.DataBind();

        }
    }

    public void SortForFull()
    {
        List<double?> fulls = new List<double?>();
        List<StudentInfo> tempStu = new List<StudentInfo>();

        foreach (StudentInfo s in studentInfo)
        {
            double? fumark = GetStudentFull(s.RID);
            int index = GetFullindex(fulls, fumark);
            fulls.Insert(index, fumark);
            tempStu.Insert(index, s);
        }
        studentInfo = tempStu;
    }

    private int GetFullindex(List<double?> fulls, double? score)
    {
        for (int i = 0; i < fulls.Count; i++)
        {
            if (score > fulls[i])
            {
                return i;
            }
        }
        return fulls.Count;
    }
    private double? GetStudentFull(int stuID)
    {
        double? full = 0;
        if (eexam == 1)
        {
            foreach (GradeScoreEducation g in gse)
            {
                if (g.StudentID == stuID && g.Score != null)
                {
                    full += g.Score;
                }
            }
        }
        else
        {
            foreach (ExamDetailSchool g in eds)
            {
                if (g.StudentID == stuID && g.Score != null)
                {
                    full += g.Score;
                }
            }
        }
        return full;
    }

    [WebMethod]
    public static string GetExcel(ExcelTableCell[][] cells)
    {
        string temp_path = HttpContext.Current.Server.MapPath("../../../");
        string save_path = temp_path + "\\download\\" + "a.xls";
        HttpContext.Current.Session["filepath"] = save_path;
        Meqs.Utilities.ExcelUtil.ExcelExport(save_path, "test", 1, 1, cells);
        return "";
    }
}
